<?
	global $table_edit, $config_xml_list, $config_xml_edit, $db;
	
	$table_edit = "images_upload";
	
	$sql = "select * from config_view where active = 1 and type = 1 and alias = '".$table_edit."'";
	//echo $sql;
	$arr = $db->getRow($sql);
	$config_xml_list = isset($arr)?$arr['values']:'11111';
	
	$sql = "select * from config_view where active = 1 and type = 2 and alias = '".$table_edit."'";
	//echo $sql;
	$arr = $db->getRow($sql);
	$config_xml_edit = isset($arr)?$arr['values']:'11111';
	//echo $config_xml_edit;
?>

<?
	include("ckeditor_config.php");	
switch($act){
	
	case "list_full":
	ShowListFull();	
	$tpl="list_full";
	break;
	
	case "add_full":
	case "edit_full":	
	EditFull();
	$title_page = "Chỉnh Sửa";
	$tpl="edit_full";
	break;

	
	case "addsm_full":
	case "editsm_full":
	EditSMFull();
	break;
	
	case "del":
	Del();
	break;
		
	case "changeactive":
	ChangeActive("active");
	break;
	
	case "changestatus":
	ChangeActive("status");
	break;
	
	case "dellist":
	DelList();
	break;
	
	case "show":
	ChangeShowList("active");
	break;
	
	case "hide":
	ChangeHideList("active");
	break;
		
	case "order":
	Order();
	break;
	
	default:
	ShowListFull();
	$title_page = "CMS - Thành Viên";
	$tpl="list_full";
		
}
function EditSMFull()
{
	global $db,$act, $FullUrl, $table_edit;	
	
	$sql = "select column_name from information_schema.columns where table_name='".$table_edit."'";
	
	$arr_table = $db->getAll($sql);
	
	$arr = NULL;
	
	for($i = 0; $i < count($arr_table);$i++)
	{
		$name_label = $arr_table[$i]['column_name'];
		//$type_label = $arr_table[$i]['data_type'];
		
		if($name_label == 'cid')
		{
			$arr = ArrPostClean2($arr, 'cid', 'cat');
		}
		else if($name_label == 'active')
		{
			$arr = ArrPostCleanActive($arr, $name_label);
		}
		else if($name_label == 'img')
		{
			if(isset($_FILES['img']['name'] ) && $_FILES['img']['size']>0){
				$img = $_FILES['img']['name'];
				$start = strpos($img,".");
				$type = substr($img,$start,strlen($img));
				CheckUpload($type);
				
				$filename = strtolower(substr($img, 0,$start));
				$filename = RenameFile($filename).$type;
				if(file_exists( "./upload/images/" . $filename)){
					$filename = substr($img, 0, $start);
					$filename = $_POST["unique_key_vn"] . '-' . time() . $type;
					
				}
				else
				{
					// Khong co
				}
				copy($_FILES['img']['tmp_name'], "./upload/images/" . $filename) ;
				$arr['img'] = "upload/images/" . $filename;
			}
		}
		else
		{
			$arr = ArrPostClean($arr, $name_label);
		}

	}
	// Phần mở rộng của table
	
	$value_answer = GetAnswer();	
	if($value_answer != '')
		$arr['answer'] = $value_answer;
		
	// Kết thúc phần mở rộng
	
	if ($act=="addsm_full")
	{
		// Xử lý Image
		$postId = vaInsert($table_edit,$arr);
		$msg="Add successfully";				
	}
	else
	{
		// Xử lý Image
		$id=$_POST['id'];
		if (isset($arr['img'])){
			$sqlstmt="select img from ".$table_edit." where id=$id";
			$r = $db->getRow($sqlstmt);
			if(file_exists($r['img'])) unlink($r['img']);
		}
		vaUpdate($table_edit, $arr,' id='.$id);	
		$msg="Đã Edit successfully";	
	}
	
	$msg="Add successfully";
	$_SESSION['mess'] = $msg;
	$page="admin-pro.html?do=".$table_edit. (isset($_GET['page'])?'&page='.$_GET['page']:'');
	page_transfer2($page);		
}


?>